<?php
/**
 * @autor Albert Moreno Vendrell
 */
class Template extends Core {
    private $path;
    private $content  = array();
    
    public function __construct() {
        $this->path = APP_BASE_PATH."/templates";
    }
    
    /**
     * @param string $nombre            
     * @param mixed $datos              
     * @autor                           Albert Moreno Vendrell
     */
    public function add($name, $datos){
        $this->content[$name] = $datos;
    }
    
    /**
     * @param array $content          
     * @autor                           Albert Moreno Vendrell
     */
    public function add_array($content, $extras){
        foreach($content as $key=>$value){
            if(isset($extras['prefijo']) && $extras['prefijo']!=""){
                $key    = $extras['prefijo'].$key;
            }
            $this->add($key, $value);
        }
    }
    
    /**
     * @param string $template         
     * @autor                           Albert Moreno Vendrell
     */
    public function view($template){
        $template  = $this->path."/".$template.".tpl.php";
        
        foreach($this->content as $key=>$value){
            $$key = $value;
        }
        
        if (is_file($template)) {
            include($template);
        }else{
            throw new Exception($template." template not found");
        }
    }
    
    /**
     * @autor                           Albert Moreno Vendrell
     */
    public function return($template){
        $template  = $this->path."/".$template.".tpl.php";
        
        foreach($this->content as $key=>$value){
            $$key = $value;
        }
        
        if (file_exists($template)) {
            ob_start();
            include($template);
            return ob_get_clean();
        }else{
            throw new Exception($template." template not found");
        }
    }
}
